package com.ruoyi.chat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.core.vo.FriendVO;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.chat.mapper.FriendshipMapper;
import com.ruoyi.chat.domain.Friendship;
import com.ruoyi.chat.service.IFriendshipService;
import org.springframework.util.CollectionUtils;

/**
 * 朋友关系Service业务层处理
 * 
 * @author zhc
 * @date 2025-05-31
 */
@Service
public class FriendshipServiceImpl implements IFriendshipService 
{
    @Autowired
    private FriendshipMapper friendshipMapper;

    /**
     * 查询朋友关系
     * 
     * @param id 朋友关系主键
     * @return 朋友关系
     */
    @Override
    public Friendship selectFriendshipById(String id)
    {
        return friendshipMapper.selectFriendshipById(id);
    }

    /**
     * 查询朋友关系列表
     * 
     * @param friendship 朋友关系
     * @return 朋友关系
     */
    @Override
    public List<Friendship> selectFriendshipList(Friendship friendship)
    {
        return friendshipMapper.selectFriendshipList(friendship);
    }

    /**
     * 新增朋友关系
     * 
     * @param friendship 朋友关系
     * @return 结果
     */
    @Override
    public int insertFriendship(Friendship friendship)
    {
        return friendshipMapper.insertFriendship(friendship);
    }

    /**
     * 修改朋友关系
     * 
     * @param friendship 朋友关系
     * @return 结果
     */
    @Override
    public int updateFriendship(Friendship friendship)
    {
        return friendshipMapper.updateFriendship(friendship);
    }

    /**
     * 批量删除朋友关系
     * 
     * @param ids 需要删除的朋友关系主键
     * @return 结果
     */
    @Override
    public int deleteFriendshipByIds(String[] ids)
    {
        return friendshipMapper.deleteFriendshipByIds(ids);
    }

    /**
     * 删除朋友关系信息
     * 
     * @param id 朋友关系主键
     * @return 结果
     */
    @Override
    public int deleteFriendshipById(String id)
    {
        return friendshipMapper.deleteFriendshipById(id);
    }

    @Override
    public boolean isAlreadFriend(Long userId, String id) {
        return friendshipMapper.isAlreadFriend(userId,id);
    }

    @Override
    public Boolean addFriend(Friendship friendship) {
        return friendshipMapper.addFriend(friendship);
    }

    @Override
    public List<Friendship> queryFriend() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Friendship friendship = new Friendship();
        friendship.setMyId(String.valueOf(loginUser.getUserid()));
        return friendshipMapper.selectFriendshipList(friendship);
    }

    @Override
    public Map<String, Friendship> convertToMap(List<Friendship> friendships) {
        if(CollectionUtils.isEmpty(friendships)){
            return new HashMap<>();
        }
        return friendships.stream().collect(Collectors.toMap(Friendship::getFriendId, Function.identity()));
    }
}
